#include "MenuScene.h"



MenuScene::MenuScene()
{
	_cursorIndex=0;
	_startTime1=GetTickCount();
	_startTime2=GetTickCount();
	_lock=false;

	ResourceManager::GetInstance()->GetSound(ResourceManager::GetInstance()->SoundName.MenuMusic)->Play(true);
}

MenuScene::~MenuScene()
{
}




void MenuScene::Update(float DeltaTime)
{
	
	_ProcessInput();
	
	
}

void MenuScene::Render()
{
	ResourceManager *_ResourceManager=ResourceManager::GetInstance();

	_d3ddv->Clear( 0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );

	_ResourceManager->GetSurface(_ResourceManager->SurfaceName.MenuScene_Background)->Draw(_backBuffer,NULL,NULL);


	_spriteHandler->Draw(_ResourceManager->GetTexture(_ResourceManager->TextureName.MenuScene_Newgame)->Get_Texture(),0,0,&D3DXVECTOR3(605,314,0),D3DCOLOR_XRGB(253, 253, 253));
	_spriteHandler->Draw(_ResourceManager->GetTexture(_ResourceManager->TextureName.MenuScene_Loadmap)->Get_Texture(),0,0,&D3DXVECTOR3(605,349,0),D3DCOLOR_XRGB(253, 253, 253));
	_spriteHandler->Draw(_ResourceManager->GetTexture(_ResourceManager->TextureName.MenuScene_Help)->Get_Texture(),0,0,&D3DXVECTOR3(605,384,0),D3DCOLOR_XRGB(253, 253, 253));
	_spriteHandler->Draw(_ResourceManager->GetTexture(_ResourceManager->TextureName.MenuScene_Exit)->Get_Texture(),0,0,&D3DXVECTOR3(605,419,0),D3DCOLOR_XRGB(253, 253, 253));
	_spriteHandler->Draw(_ResourceManager->GetTexture(_ResourceManager->TextureName.MenuScene_Cursor)->Get_Texture(),0,0,&D3DXVECTOR3(550,314+35*_cursorIndex,0),D3DCOLOR_XRGB(253, 253, 253));

}

void MenuScene::Leave()
{
	// Giai phong bo nho

	ResourceManager::GetInstance()->GetSound(ResourceManager::GetInstance()->SoundName.MenuMusic)->Stop();
}

void MenuScene::Pause()
{
	// Tam thoi ngung hoat dong
	ResourceManager::GetInstance()->GetSound(ResourceManager::GetInstance()->SoundName.MenuMusic)->Stop();
}

void MenuScene::Resume()
{
	// Tai hoat dong
	ResourceManager::GetInstance()->GetSound(ResourceManager::GetInstance()->SoundName.MenuMusic)->Play(true);
}

void MenuScene::_ProcessInput()
{

	if (CGame::GetInstance()->GetInput()->IsKeyDown(DIK_DOWN))
	{
		float delta=GetTickCount()-_startTime1;
		if(delta>200)
		{

				switch(_cursorIndex)
				{
					case 0:  _cursorIndex=1; break;
					case 1:  _cursorIndex=2; break;
					case 2:  _cursorIndex=3; break;
					case 3:  _cursorIndex=0; break;
				}
		
				ResourceManager::GetInstance()->GetSound(ResourceManager::GetInstance()->SoundName.MenuChange)->Play(false);
				_startTime1=GetTickCount();

		
		}
	}
	else if(CGame::GetInstance()->GetInput()->IsKeyDown(DIK_UP))
	{
		
		float delta=GetTickCount()-_startTime1;
		if(delta>200)
		{
				switch(_cursorIndex)
				{
					case 0:  _cursorIndex=3; break;
					case 1:  _cursorIndex=0; break;
					case 2:  _cursorIndex=1; break;
					case 3:  _cursorIndex=2; break;
				}
		
				ResourceManager::GetInstance()->GetSound(ResourceManager::GetInstance()->SoundName.MenuChange)->Play(false);
				_startTime1=GetTickCount();
	
		
		}
	}
	else if(CGame::GetInstance()->GetInput()->IsKeyDown(DIK_RETURN))
	{
			float delta=GetTickCount()-_startTime2;
			if(delta>200)
			{
				if(_lock==false)
				{
					switch(_cursorIndex)
					{
					case 0:
						{
							SceneManager::GetInstance()->PushScene((Scene*) new GamePlayScene("..\\Resources\\Images\\Map.txt","..\\Resources\\Images\\QuadTree.txt"));
							SceneManager::GetInstance()->PushScene((Scene*) new LoadingScene(3));
							break;
						}
					case 3: PostMessage(_hWnd,WM_QUIT,0,0); break;
					}					
				}

				else _lock=false;
			}
			//if(delta>300 && _lock==true)
			//{
			//	_lock=false;
			//}
			_startTime2=GetTickCount();
			ResourceManager::GetInstance()->GetSound(ResourceManager::GetInstance()->SoundName.MenuChange)->Play(false);
	}
}

void MenuScene::SetLock(bool lock)
{
	_lock=lock;
}

